#include "head.h"

#if 0
//1.0
//客户端cd 命令
//参数：fd_net文件描述符,filename为用户输入的目标文件，ps为虚拟路径所在结构体
//返回值：0表示成功，-1表示失败
int cd(int fd_net, char *filename, process_stat_t *ps,transport_protocol_t *tp){
    //判断filename是否为.
    if(strcmp(filename, ".") == 0){
        //cd .
        return 0;
    }else if(strcmp(filename, "..") == 0){//判断filename是否为..
                                          //cd ..
        cd_up(ps);
        memcpy(tp->curr_path,ps->curr_path,sizeof(tp->curr_path));
        return 3;
    }else {
        printf("cd要发送了\n");
        //接收服务器反馈
        int flag=0;
        recv(fd_net, &flag, sizeof(int), MSG_WAITALL);
        printf("flag:%d\n",flag);
        if(flag == 0){
            //命令执行成功
            //改变当前路径
            strcat(ps->curr_path, "/");
            strcat(ps->curr_path, filename);
            memcpy(tp->curr_path,ps->curr_path,sizeof(tp->curr_path));
            printf("cd: %s\n",ps->curr_path);
            return 0;
        }else if(ps->flag == 1){
            //命令执行失败
            printf("文件夹不存在\n");
            return -1;
        }
    }
    //其他情况
    return 2;
}
#endif
/**
 * 实现客户端的cd命令功能
 * 该函数尝试在服务器端改变当前目录，并同步更新客户端的当前目录信息
 * 
 * @param fd_net 服务器端的文件描述符，用于通信
 * @param filename 要改变到的目录名
 * @param ps 进程状态结构体指针，用于记录和传递状态信息
 * @param tp 传输协议结构体指针，包含当前路径等信息
 * 
 * @return 操作执行的结果，-1表示失败
 */
int cd(int fd_net, char *filename, process_stat_t *ps, transport_protocol_t *msg){
    //栈数组
    char *stack[1000];
    //栈顶索引
    int top = -1;

    char buf[100] = { 0 };
    strcat(buf, ps->curr_path);
    if (strcmp(buf, "/") != 0){
        strcat(buf, "/");
    }
    strcat(buf, filename);

    char *token = strtok(buf, "/");

    while(token != NULL){
        if (strcmp(token, ".") == 0){
            token = strtok(NULL,"/");
            continue;
        }else if(strcmp(token, "..") == 0){
            //出栈
            if(top != -1){
                top--;
            }
        }else{
            //入栈
            stack[++top] = (char *)malloc(strlen(token) + 1);
            //判断是否分配成功
            ERROR_CHECK(stack[top], NULL, "malloc error");
            strcpy(stack[top], token); 
        }
        token = strtok(NULL,"/");
    }

    //获取栈中的数组

    //获取总共的长度
    int len = 1;    
    for (int i = 0; i <= top; i++){
        //加2 是因为要加一个/
        len += strlen(stack[i]) + 2;
    }

    char *ret_path = (char*)malloc(len + 2);
    ERROR_CHECK(ret_path, NULL, "malloc error");
    
    bzero(ret_path, sizeof(ret_path));

    for (int i = 0; i <= top; i++){
        if (i > 0){
            strcat(ret_path, "/");
        }
        strcat(ret_path, stack[i]);
        free(stack[i]);
    }

    char *tmp = (char*)malloc(len + 1);
    ERROR_CHECK(tmp, NULL, "malloc error");
    bzero(tmp, sizeof(tmp));
    strcat(tmp, "/");
    strcat(tmp, ret_path);

    free(ret_path);

    //发送路径
    send(fd_net, tmp, len + 1,  0);


    // 接收服务器反馈
    int flag = 2;
    recv(fd_net, &flag, sizeof(int), 0);
    // printf("flag:%d\n", flag);
    if (flag == 0){
        //printf("ps->curr_path: %s\n", ps->curr_path);
        memcpy(ps->curr_path, tmp, sizeof(ps->curr_path));
        memcpy(msg->curr_path, tmp, sizeof(msg->curr_path));
        //printf("ps->curr_path: %s\n", ps->curr_path);
        return 0;
    }else{
        printf("文件夹不存在\n");
        LOG_WRITE(1, "Folder does not exist");
        return -1;
    }

    return 0; 
}

